package com.selangshiwo;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;

public class Util {
	
	public static List<String> regex(String in , String regex ){
		List<String> out = new ArrayList<String>();
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(in);
		int i = 0 ;
		if(matcher.groupCount() > 0){
			while(matcher.find()){
				String str = matcher.group(1);
				out.add(i , str);
			}
		}
		return out ;
	}
	
	public static String entity2String(HttpEntity en){
		try {
			return EntityUtils.toString(en);
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static String getUrlScheme(String url){
		Pattern pattern = Pattern.compile("http([^:\\/]*)");
		Matcher matcher = pattern.matcher(url);
		String result = "";
		if(matcher.find())
			result = matcher.group(0);
		
		return result ;
	}
	
	public static String getUrlHost(String url){
		Pattern pattern = Pattern.compile(":\\/\\/([^\\/]*)");
		Matcher matcher = pattern.matcher(url);
		String result = "";
		if(matcher.find())
			result = matcher.group(1);
		
		return result ;
	}
	
	public static String getUrlPath(String url){
		//有高手修改下该正则
		String host = getUrlHost(url);
		Pattern pattern = Pattern.compile(host + "([^\\?]*)");
		Matcher matcher = pattern.matcher(url);
		String result = "";
		if(matcher.find())
			result = matcher.group(1);
		
		return result ;
		
	}
	
	public static String getUrlParams(String url){
		Pattern pattern = Pattern.compile("\\?([a-zA-Z=\\d%#*&-.()]*)?");		//   (//?[a-z=//d%#*&-]*)?
		Matcher matcher = pattern.matcher(url);
		String result = "";
		if(matcher.find())
			result = matcher.group(1);
		
		return result ;
		
	}
	
	public static String getHtmlBody(String htmlText){
		if(htmlText.indexOf("<body>") > 0){
			String strRegex = "(?is)(?<=<body>).*(?=</body>)";			//body 部分正则
			return regex(htmlText , strRegex).get(0);
		}else{
			return htmlText ;
		}
	}
	
	public static String getParams(String context , int index){
		String strRegex =  "\\((.*?)\\)";			//取两( )间值,即参数值  "(?<=\\().*(?=\\))"
		return regex(context , strRegex).get(index);
	}

	public static Map<String , ?> string2map(String in){
		return (Map<String, ?>) JSONObject.parse(in) ;		
	}
	
	
	/**
	 * base64
	 *		openJDK中无此包
	 */
	public static String base42Encode(byte[] bstr){
		return new sun.misc.BASE64Encoder().encode(bstr);
	}

	public static byte[] base64Decode(String str){
		byte[] out = null ;
		try{
			out = new sun.misc.BASE64Decoder().decodeBuffer(str);
		}catch(Exception e){
			e.printStackTrace();
		}

		return out ;
	}
	
	public static String hex_sha1(String in) throws NoSuchAlgorithmException{
		String out = "";
		String encType = "SHA";
		MessageDigest md = MessageDigest.getInstance(encType);
		md.update(in.getBytes());
		out = byte2hex(md.digest());
		return out ;
	}
	
	public static String byte2hex(byte[] in){
		String out = "";
		String tmp = null ;
		for (int i=0 , len = in.length ;i < len ; i++) {
			tmp=(Integer.toHexString(in[i] & 0xFF));
			if (tmp.length()==1) {
				out+="0";
			}
			out+=tmp;
		}
		return out;
	}
	static boolean debug = false ;
	
	public static boolean onDebug(){
		debug = !debug ;
		return debug ;
	}
	
	
	public static void debug(Object obj){
		if(debug)
			System.out.println("debug:" + obj);
	}
	
	public static void err(Object obj){
		System.err.println("ERROR : *******" + obj.toString() + "**************************");
	}
	
	public static void log(Object obj){
		System.out.println( obj );
	}
	
}
